<?php

/**
 * This is the model class for table "node".
 *
 * The followings are the available columns in table 'node':
 * @property string $id
 * @property string $cid
 * @property string $uid
 * @property string $fid
 * @property integer $tid
 * @property string $title
 * @property string $body
 * @property integer $status
 * @property integer $access
 * @property integer $level
 * @property string $visit
 * @property string $create_time
 * @property string $update_time
 */
class Node extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Node the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'node';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('uid, tid, title, body, status, level, create_time, update_time', 'required'),
			array('tid, status, access, level', 'numerical', 'integerOnly'=>true),
			array('cid, uid, fid, visit, create_time, update_time', 'length', 'max'=>10),
			array('title', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, cid, uid, fid, tid, title, body, status, access, level, visit, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'uid'),
			'category' => array(self::BELONGS_TO, 'Category', 'cid'),
			'fnode'=>array(self::BELONGS_TO, 'NOde', 'fid'),
		
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cid' => '分类',
			'uid' => '作者',
			'fid' => '父类',
			'tid' => '类型',
			'title' => '名称',
			'body' => '内容',
			'status' => '状态',
			'access' => '权限',
			'level' => '成熟度',
			'visit' => '访问量',
			'create_time' => '创建时间 ',
			'update_time' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('cid',$this->cid,true);
		$criteria->compare('uid',$this->uid,true);
		$criteria->compare('fid',$this->fid,true);
		$criteria->compare('tid',$this->tid);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('access',$this->access);
		$criteria->compare('level',$this->level);
		$criteria->compare('visit',$this->visit,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取状态文本
	 */
	public function getStatusText()
	{
		$options = DefineBox::$nodeStatusArray;
		return isset($options[$this->status]) ? $options[$this->status] : "未知($this->status)";
	}
	
	/**
	 * 获取权限文本
	 */
	public function getAccessText()
	{
		$options = DefineBox::$nodeAccessArray;
		return isset($options[$this->access]) ? $options[$this->access] : "未知($this->access)";
	}
	
}